<?php
namespace app\controller\api\module;

use think\response\Json;

class ReceptionBaseController extends BaseController
{
    protected array $userInfo;
    protected string $token;

    protected function initialize()
    {
        $this->token = $this->getToken(request()->header('token') ?: '', request()->header('refreshToken') ?: '');
        $this->userInfo = $this->token ? getAPICaches($this->token, []) : [];
        empty($this->userInfo) and responseMsgExit('token已失效，请重新登录', 1001);
    }

    /**
     * 响应错误数据
     * @param string $msg
     * @param int $code
     * @param array $data
     * @param array $header
     * @return Json
     */
    protected function error(string $msg = '系统繁忙',  array $data = [], int $code = 500, array $header = []): Json
    {
        return responseMsg($msg, $code, $data, array_merge($header, ['token' => $this->token]));
    }

    /**
     * 响应正确数据
     * @param string $msg
     * @param int $code
     * @param array $data
     * @param array $header
     * @return Json
     */
    protected function success(string $msg = '成功',  array $data = [], int $code = 200, array $header = []): Json
    {
        return responseMsg($msg, $code, $data, array_merge($header, ['token' => $this->token]));
    }

    /**
     * 响应数据
     * @param string $msg
     * @param int $code
     * @param array $data
     * @param array $header
     * @return Json
     */
    protected function response(string $msg, int $code, array $data = [], array $header = []): Json
    {
        return responseMsg($msg, $code, $data, array_merge($header, ['token' => $this->token]));
    }


}